﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Collections.Generic;

namespace Utils
{
    public class FlexiblePolygon
    {
        public FlexiblePolygon() { }

        private List<Dian> dianList = new List<Dian>();
        private List<Line> links = new List<Line>();

        public Canvas showCanvas;

        public void showAll()
        {
            showCanvas.Children.Clear();
            foreach (Line ll in Links)
                showCanvas.Children.Add(ll);
            foreach (Dian dd in DianList)
                showCanvas.Children.Add(dd.rectangle);
        }

        public void add(Dian d)
        {           
            int index = DianList.Count;
            if(index > 0)
            {
                Line l = new Line();
                l.X1 = DianList[index - 1].Point.X;
                l.Y1 = DianList[index - 1].Point.Y;
                l.X2 = d.Point.X;
                l.Y2 = d.Point.Y;
                l.Stroke = System.Windows.Media.Brushes.Red;
                l.StrokeThickness = 3;
                l.StrokeDashArray = new DoubleCollection() { 5, 3 };

                Line ll = new Line();
                ll.X2 = DianList[0].Point.X;
                ll.Y2 = DianList[0].Point.Y;
                ll.X1 = d.Point.X;
                ll.Y1 = d.Point.Y;
                ll.Stroke = System.Windows.Media.Brushes.Red;
                ll.StrokeThickness = 3;
                ll.StrokeDashArray = new DoubleCollection() { 5, 3 };

                l.MouseDown += l_MouseDown;
                ll.MouseDown += l_MouseDown;

                if(Links.Count > 0)
                    Links.RemoveAt(Links.Count-1);

                Links.Add(l);
                Links.Add(ll);
            }
            dianList.Add(d);        
        }

        private void l_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.ClickCount >= 2)
            {
                Line l = sender as Line;
                Point p = new Point();
                p.X = (l.X2 - l.X1) / 2 + l.X1;
                p.Y = (l.Y2 - l.Y1) / 2 + l.Y1;

                Dian d = new Dian(p);
                d.Dad = this;

                for (int i = 0; i < Links.Count; i++)
                {
                    if (Links[i] == l)
                    {
                        Line l1 = new Line();
                        l1.X1 = DianList[i].Point.X;
                        l1.Y1 = DianList[i].Point.Y;
                        l1.X2 = d.Point.X;
                        l1.Y2 = d.Point.Y;
                        l1.Stroke = System.Windows.Media.Brushes.Red;
                        l1.StrokeThickness = 3;
                        l1.StrokeDashArray = new DoubleCollection() { 5, 3 };

                        Line ll = new Line();
                        if (i == Links.Count - 1)
                        {                          
                            ll.X2 = DianList[0].Point.X;
                            ll.Y2 = DianList[0].Point.Y;
                            ll.X1 = d.Point.X;
                            ll.Y1 = d.Point.Y;
                            ll.Stroke = System.Windows.Media.Brushes.Red;
                            ll.StrokeThickness = 3;
                            ll.StrokeDashArray = new DoubleCollection() { 5, 3 };
                        }
                        else {
                            ll.X2 = DianList[i + 1].Point.X;
                            ll.Y2 = DianList[i + 1].Point.Y;
                            ll.X1 = d.Point.X;
                            ll.Y1 = d.Point.Y;
                            ll.Stroke = System.Windows.Media.Brushes.Red;
                            ll.StrokeThickness = 3;
                            ll.StrokeDashArray = new DoubleCollection() { 5, 3 };
                        }

                        l.MouseDown += l_MouseDown;
                        ll.MouseDown += l_MouseDown;

                        DianList.Insert(i+1,d);
                        Links.RemoveAt(i);

                        Links.Insert(i,ll);
                        Links.Insert(i,l);

                        break;
                    }
                }
                showAll();
            }
        }

        public void tuodong(Dian d)
        { 
            int index = -1;
            for (int i = 0; i < dianList.Count; i++)
            {
                if (DianList[i] == d)
                {
                    index = i;
                    break;
                }
            }

            if (index != -1)
            {
                if (index == 0)
                {
                    Links[Links.Count - 1].X2 = Links[0].X1 = d.Point.X;
                    Links[Links.Count - 1].Y2 = Links[0].Y1 = d.Point.Y;
                }
                else
                {
                    Links[index - 1].X2 = Links[index].X1 = d.Point.X;
                    Links[index - 1].Y2 = Links[index].Y1 = d.Point.Y;
                }
               
            }
        }

        public List<Dian> DianList
        {
            get { return dianList; }
            set { dianList = value; }
        }
        public List<Line> Links
        {
            get { return links; }
            set { links = value; }
        }
    }

    public class Dian
    {
        private Point point;
        private FlexiblePolygon dad;

        public FlexiblePolygon Dad
        {
            get { return dad; }
            set { dad = value; }
        }

        public Point Point
        {
            get { return point; }
            set 
            {
                if (value != point)
                {
                    point = value;
                    Dad.tuodong(this);
                }
            }
        }
        public Rectangle rectangle;

        public Dian() { }

        public Dian(Point p)
        {
            point = p;

            rectangle = new Rectangle();
            Canvas.SetTop(rectangle, point.Y - 5);
            Canvas.SetLeft(rectangle, point.X - 5);
            rectangle.Width = 10;
            rectangle.Height = 10;
            rectangle.DataContext = this;
            rectangle.Fill = System.Windows.Media.Brushes.Blue;
            // rectangle.MouseMove += new MouseEventHandler(yidong);
        }
        public Dian(double x, double y)
        {
            point = new Point(x, y);

            rectangle = new Rectangle();
            Canvas.SetTop(rectangle, point.Y - 5);
            Canvas.SetLeft(rectangle, point.X - 5);
            rectangle.Width = 10;
            rectangle.Height = 10;
            rectangle.Fill = System.Windows.Media.Brushes.Blue;
            rectangle.DataContext = this;
        }

        public double X
        {
            get
            {
                return Canvas.GetLeft(rectangle);
            }
        }

        public double Y
        {
            get
            {
                return Canvas.GetTop(rectangle);
            }
        }

    }
}
